package com.jsjosem.aftersales.ui.worktickets;

import static com.zx.sdk.api.ZXManager.TAG;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.CustomerListAdapter;
import com.jsjosem.aftersales.data.response.CustomerResponse;
import com.jsjosem.aftersales.request.CustomerRequest;
import com.jsjosem.aftersales.utils.PageUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.List;

public class CustomerListActivity extends AppCompatActivity {

    private ListView customerLv;
    private ImageView imgBack;
    private final CustomerRequest customerApi = new CustomerRequest();
    private EditText searchEt;
    private TextView searchTv;
    private String searchContent="";
    private Integer totalPage;
    private Integer currentPage = 1;
    private List<CustomerResponse> customerResponseList;
    private CustomerListAdapter adapter;
    private SmartRefreshLayout smartRefreshLayout;
    private Dialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_list);
        initView();
        loadingShow();
        initData();

        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            if (searchContent.equals("")) {
                getData(currentPage++, "");
            } else {
                getData(currentPage++, searchContent);
            }
        });

        //listview 的点击事件
        customerLv.setOnItemClickListener((adapterView, view, position, id) -> {
            CustomerResponse customerItem = customerResponseList.get(position);
            Intent intent = new Intent(CustomerListActivity.this, MaintainActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("customerItem", customerItem);
            intent.putExtras(bundle);

            this.setResult(RESULT_OK, intent);
            finish();
        });

        searchTv.setOnClickListener(v -> {
            if (searchEt.getText().toString().equals("")) {
                Toast.makeText(CustomerListActivity.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
            } else {
                smartRefreshLayout.setNoMoreData(false);
                currentPage = 1;
                customerResponseList.clear();
                searchContent= searchEt.getText().toString();
                getData(currentPage++, searchEt.getText().toString());
            }
        });

        //搜索框的监听事件
        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.e(TAG, "beforeTextChanged: " + "输入后" + s.toString());
                if (s.toString().equals("") && !searchContent.equals("")) {
                    customerResponseList.clear();
                    searchContent= "";
                    currentPage = 1;
                    getData(currentPage++, "");
                    smartRefreshLayout.setNoMoreData(false);
                }
            }

        });

        imgBack.setOnClickListener(view -> {
            finish();
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        getData(currentPage++, "");
    }

    private void initView() {
        imgBack = findViewById(R.id.customer_list_img_back);
        customerLv = findViewById(R.id.customer_list_lv);
        searchEt = findViewById(R.id.search);
        smartRefreshLayout = findViewById(R.id.refreshLayout);
        searchTv=findViewById(R.id.search_tv);
    }

    private void initData() {
        customerResponseList = new ArrayList<>();
        adapter = new CustomerListAdapter(CustomerListActivity.this, R.layout.customer_item_list, customerResponseList);
        customerLv.setAdapter(adapter);
    }

    private void getData(int currentPage, String keyword) {
        customerApi.customers(currentPage, 10, keyword, (res) -> {
            totalPage = PageUtils.totalPage(10, res.getCount());
            if(res.getData().size()<1){
                Toast.makeText(this, "没有找到相关客户", Toast.LENGTH_LONG).show();
                adapter.notifyDataSetChanged();
            }
            if (currentPage > totalPage) {
                smartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                customerResponseList.addAll(res.getData());
                adapter.notifyDataSetChanged();
                smartRefreshLayout.finishLoadMore();
                loadingDialog.dismiss();
            }
        }, failedMessage -> Toast.makeText(this, failedMessage.toString(), Toast.LENGTH_LONG).show());

    }

    private void loadingShow() {
        loadingDialog = new Dialog(this, R.style.normal_dialog_style);
        View loadingView = View.inflate(this, R.layout.dialog_loading, null);
        loadingDialog.setContentView(loadingView);
        //使得点击对话框外部不消失对话框
        loadingDialog.setCanceledOnTouchOutside(false);
        AVLoadingIndicatorView avi = loadingView.findViewById(R.id.avi);
        avi.show();
        loadingDialog.show();
    }


}